<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Supervisor;
use App\Thesis;
use App\Student;
use App\Comment;
use \PhpOffice\PhpWord\TemplateProcessor;
use App\Description;
use App\Proposal;
use App\Paper;
use App\SupervisorComment;
use App\ReviewerComment;
use App\DefenseComment;
use \Chumper\Zipper\Zipper;

use Storage;

class DownLoadController extends Controller
{

    public function all(Request $request) {
        $class_id = $request->class_id;
        $statistic_type = $request->statistic_type;

        $start_id = 0;
        $end_id = 100000;
        $output_class = "";
        $paper_file_path_prefix = "";
        if ($class_id == 1) {
            $start_id = 1;
            $end_id = 69;
            $output_class = "普通计科";
            $paper_file_path_prefix = "0809_080901_";

        } else if ($class_id == 2) {
            $start_id = 70;
            $end_id = 148;
            $output_class = "外包计科";
            $paper_file_path_prefix = "0809_080901_";
        } else if ($class_id == 3) {
            $start_id = 149;
            $end_id = 251;
            $output_class = "外包软工";
            $paper_file_path_prefix = "0809_080902_";
        } else if ($class_id == 4) {
            $start_id = 252;
            $end_id = 307;
            $output_class = "数应";
            $paper_file_path_prefix = "0701_070101_";
        } else if ($class_id == 5) {
            $start_id = 308;
            $end_id = 362;
            $output_class = "网工";
            $paper_file_path_prefix = "0809_080903_";
        }

        if ($statistic_type == 1) {
            $output_class .= "_成绩单.zip";
        } else if ($statistic_type == 2) {
            $output_class .= "_评阅人评审表.zip";
        } else if ($statistic_type == 3) {
            $output_class .= "_答辩资格审查表.zip";
        } else if ($statistic_type == 4) {
            $output_class .= "_论文.zip";
        }

        $files = Array();

        for ($i = $start_id; $i <= $end_id; $i++) {
            if ($statistic_type == 1) {
                $file_path = $this->download_cjd($i, 0);
                $files = array_merge($files, glob($file_path));
            } else if ($statistic_type == 2) {
                $file_path = $this->down_pyrpsb($i, 0);
                $files = array_merge($files, glob($file_path));
            } else if ($statistic_type == 3) {
                $file_path = $this->down_dbzgscb($i, 0);
                $files = array_merge($files, glob($file_path));
            } else if ($statistic_type == 4) {
                $file_path = $this->down_paper($i, $paper_file_path_prefix);
                $files = array_merge($files, glob($file_path));
            }
        }
        $zipper = new Zipper();

        $zipper->make(storage_path() . '/' . $output_class)->add($files);
        $zipper->close();
        return response()->download(storage_path() . "/" . $output_class);

    }

    public function down_paper($student_id, $paper_file_path_prefix) {
        $student = Student::find($student_id);
        $current = $student->paper()->where('current', '>', 0)->first();
        $old_file_path = $current->paper;
        $suffix = preg_replace("/.*\.(\w+)/" , "\\1" ,$old_file_path);

        $disk = Storage::disk('papers');
        $new_file_path = $paper_file_path_prefix . $student->user->username . "_LW." . $suffix;
        if ($disk->exists($new_file_path)) {
            $disk->delete($new_file_path);
        }
        $disk->copy($old_file_path, $new_file_path);
        return public_path('uploads/papers/' . $new_file_path);
    }

    public function download_cjd($student_id, $download = 1)
    {
        $student = Student::find($student_id);
        $file = public_path('cjd_template.docx');
        $templateProcessor = new TemplateProcessor($file);
        $templateProcessor->setValue('major', $student->major);
        $templateProcessor->setValue('enroll_time', "20" . substr($student->user->username, 0,2));
        $templateProcessor->setValue('student_id', $student->user->username);
        $templateProcessor->setValue('student_name', $student->user->name);
        $templateProcessor->setValue('class', $student->class);
        //$templateProcessor->setValue('supervisor', $user->name);
        //$templateProcessor->setValue('jobtitle', $user->supervisor->title);
        $title = "学生暂未选题";
        $current = $student->title()->where('current', '>', 0)->first();
        if ($current) {
            $title = $current->title;
        }
        $templateProcessor->setValue('title', $title);


        $supervise_comment = $student->supervise_comment;
        $supervise_content = "";
        $supervise_grade = "";
        if ($supervise_comment) {
            $supervise_content = $supervise_comment->content;
            $supervise_grade = $supervise_comment->grade;
        }
        $templateProcessor->setValue('supervise_comment', $supervise_content);
        $templateProcessor->setValue('supervise_grade', $supervise_grade);
        $supervisor = $student->supervisors()->first();
        if ($supervisor != null) {
            $templateProcessor->setValue('supervisor', $supervisor->user->name);
            $templateProcessor->setImageValue('supervisor_image', array("path"=>public_path('signs/' . $supervisor->user->name . '.JPG'),
                "height"=>25));
            $templateProcessor->setValue('jobtitle', $supervisor->title);
        } else {
            $templateProcessor->setValue('supervisor', "");
            $templateProcessor->setValue('supervisor_image', "");
            $templateProcessor->setValue('jobtitle', "");
        }        


        $review_comment = $student->review_comment;
        $review_content = "";
        $review_grade = "";
        if ($review_comment) {
            $review_content = $review_comment->content;
            $review_grade = $review_comment->grade;
        }
        $templateProcessor->setValue('review_comment', $review_content);
        $templateProcessor->setValue('review_grade', $review_grade);
        $reviewer = $student->reviewer()->first();
        if ($reviewer != null) {
            $templateProcessor->setImageValue('reviewer', array("path"=>public_path('signs/' . $reviewer->user->name . '.JPG'),
                "height"=>25));
        } else {
            $templateProcessor->setValue('reviewer', "");
        }

        $defense_comment = $student->defense_comment;
        $defense_content = "";
        $defense_grade = "";
        if ($defense_comment) {
            $defense_content = $defense_comment->content;
            $defense_grade = $defense_comment->grade;
        }
        $templateProcessor->setValue('defense_comment', $defense_content);
        $templateProcessor->setValue('defense_grade', $defense_grade);
        $defense_teacher = $student->defense_teacher()->first();
        if ($defense_teacher != null) {
            $templateProcessor->setImageValue('defense_teacher', array("path"=>public_path('signs/' . $defense_teacher->user->name . '.JPG'),
                "height"=>25));
        } else {
            $templateProcessor->setValue('defense_teacher', "");
        }        

        if ($supervise_grade == "" || $review_grade == "" || $defense_grade == "" ) {
            $templateProcessor->setValue('total_grade', "");
            $templateProcessor->setValue('rank', "");
        } else {
            $total_grade = $supervise_grade * 0.3 + $review_grade * 0.3 + $defense_grade * 0.4;
            $templateProcessor->setValue('total_grade', $total_grade);
            $rank = "不及格";
            if ($total_grade >= 90) {
                $rank = "优秀";
            } else if ($total_grade >= 80) {
                $rank = "良好";
            } else if ($total_grade >= 70) {
                $rank = "中等";
            } else if ($total_grade >= 60) {
                $rank = "及格";
            }
            $templateProcessor->setValue('rank', $rank);
        }
        $file_path = public_path('cjd/cjd_' . $student->user->username . '.docx');
        $templateProcessor->saveAs($file_path);
        if ($download == 1) {
            return response()->download($file_path);
        } else {
            return $file_path;
        }
    }


    public function down_dbzgscb($student_id, $download = 1)
    {
        $student = Student::find($student_id);
        $file = public_path('dbzg_template.docx');
        $templateProcessor = new TemplateProcessor($file);
        $templateProcessor->setValue('major', $student->major);
        $templateProcessor->setValue('student_id', $student->user->username);
        $templateProcessor->setValue('student_name', $student->user->name);
        $templateProcessor->setValue('class', $student->class);
        //$templateProcessor->setValue('supervisor', $user->name);
        //$templateProcessor->setValue('jobtitle', $user->supervisor->title);
        $title = "学生暂未选题";
        $current = $student->title()->where('current', '>', 0)->first();
        if ($current) {
            $title = $current->title;
        }
        $templateProcessor->setValue('title', $title);


        $supervise_comment = $student->supervise_comment;
        $supervise_content = "";
        $supervise_grade = "";
        if ($supervise_comment) {
            $supervise_content = $supervise_comment->content;
            $supervise_grade = $supervise_comment->grade;
        }
        $templateProcessor->setValue('supervise_comment', $supervise_content);
        $templateProcessor->setValue('supervise_grade', $supervise_grade);
        $supervisor = $student->supervisors()->first();
        if ($supervisor != null) {
            $templateProcessor->setImageValue('supervisor_image', array("path"=>public_path('signs/' . $supervisor->user->name . '.JPG'),
                "height"=>30));
        } else {
            $templateProcessor->setValue('supervisor_image', "");
        }        

        $file_path = public_path('dbzgscb/dbzgscb_' . $student->user->username . '.docx');
        $templateProcessor->saveAs($file_path);
        if ($download == 1) {
            return response()->download($file_path);
        } else {
            return $file_path;
        }
    }


    public function down_pyrpsb($student_id, $download = 1)
    {
        $student = Student::find($student_id);
        $file = public_path('pyrpsb_template.docx');
        $templateProcessor = new TemplateProcessor($file);
        $templateProcessor->setValue('major', $student->major);
        $templateProcessor->setValue('student_id', $student->user->username);
        $templateProcessor->setValue('student_name', $student->user->name);
        $templateProcessor->setValue('reviewer', $student->reviewer()->first()->user->name);
        $templateProcessor->setValue('jobtitle', $student->reviewer()->first()->title);
        $title = "学生暂未选题";
        $current = $student->title()->where('current', '>', 0)->first();
        if ($current) {
            $title = $current->title;
        }
        $templateProcessor->setValue('title', $title);


       


        $review_comment = $student->review_comment;
        $review_content = "";
        $review_grade = "";
        if ($review_comment) {
            $review_content = $review_comment->content;
            $review_grade = $review_comment->grade;
        }
        $templateProcessor->setValue('review_comment', $review_content);
        $templateProcessor->setValue('review_grade', $review_grade);
        $reviewer = $student->reviewer()->first();
        if ($reviewer != null) {
            $templateProcessor->setImageValue('reviewer_image', array("path"=>public_path('signs/' . $reviewer->user->name . '.JPG'),
                "height"=>30));
        } else {
            $templateProcessor->setValue('reviewer_image', "");
        }

        $heavy_check_mark = "√";
        //$heavy_check_mark = "\xe2\x9c\x93";
        $ballot_box  = "\xe2\x98\x90";
        $ballot_box_with_check = "\xe2\x98\x91";
        $blank = "";
        if ($review_grade >= 80) {
            for ($i = 1; $i < 15; $i++) {
                $r = rand(0, 100) * $review_grade;
                if ($r > 3500) {
                    $templateProcessor->setValue('h' . $i, $heavy_check_mark);
                    $templateProcessor->setValue('j' . $i, $blank);
                } else {
                    $templateProcessor->setValue('h' . $i, $blank);
                    $templateProcessor->setValue('j' . $i, $heavy_check_mark);
                }
                $templateProcessor->setValue('y' . $i, $blank);
                $templateProcessor->setValue('c' . $i, $blank);
            }
        } else if ($review_grade >= 60) {
            for ($i = 1; $i < 15; $i++) {
                $r = rand(0, 100) * $review_grade;
                if ($r > 3500) {
                    $templateProcessor->setValue('j' . $i, $heavy_check_mark);
                    $templateProcessor->setValue('y' . $i, $blank);
                } else {
                    $templateProcessor->setValue('j' . $i, $blank);
                    $templateProcessor->setValue('y' . $i, $heavy_check_mark);
                }
                $templateProcessor->setValue('h' . $i, $blank);
                $templateProcessor->setValue('c' . $i, $blank);
            }
        } else {
            for ($i = 1; $i < 15; $i++) {
                $r = rand(0, 100) * $review_grade;
                if ($r > 2000) {
                    $templateProcessor->setValue('y' . $i, $heavy_check_mark);
                    $templateProcessor->setValue('c' . $i, $blank);
                } else {
                    $templateProcessor->setValue('y' . $i, $blank);
                    $templateProcessor->setValue('c' . $i, $heavy_check_mark);
                }
                $templateProcessor->setValue('h' . $i, $blank);
                $templateProcessor->setValue('j' . $i, $blank);
            }           
        }

        if ($review_grade >= 90) {
            $templateProcessor->setValue('origin1', $ballot_box_with_check);
            $templateProcessor->setValue('origin2', $ballot_box);
        } else {
            $templateProcessor->setValue('origin2', $ballot_box_with_check);
            $templateProcessor->setValue('origin1', $ballot_box);
        }
        $templateProcessor->setValue('origin3', $ballot_box);
        $templateProcessor->setValue('origin4', $ballot_box);

        $file_path = public_path('pyrpsb/pyrpsb_' . $student->user->username . '.docx');
        $templateProcessor->saveAs($file_path);
        if ($download == 1) {
            return response()->download($file_path);
        } else {
            return $file_path;
        }
    }

}
